import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Login',
        component: () =>
            import ('../components/Login.vue')
    }, {
        path: "/dashboard",
        name: "Dashboard",
        component: () =>
            import ("../components/Dashboard.vue"),
        children: [{
                path: "",
                component: () =>
                    import ("../components/Home.vue"),
                meta: [{
                    key: 1,
                    text: "首页",
                    to: {
                        path: "/dashboard"
                    }

                }],
            },
            {
                path: "/userlist",
                component: () =>
                    import ("../components/UserList.vue"),
                meta: [{
                    key: 1,
                    text: "首页",
                    to: {
                        path: "/dashboard"
                    }

                }, {
                    key: 2,
                    text: "数据管理",

                }, {
                    key: 3,
                    text: "用户列表",
                    to: {
                        path: "/userlist"
                    }

                }],
            },
            {
                path: "/addshop",
                component: () =>
                    import ("../components/AddShop.vue")
            }
        ]
    }

]

//重写路由中的push方法 避免多次点击同一位置的时候出现报错
let originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(path) {
    originalPush.call(this, path).catch(() => {})
}

const router = new VueRouter({
    routes
})



//配置全局的导航守卫
router.beforeEach(function(to, from, next) {
    console.log(to, from)
        //获取登录状态
    let loginToken = localStorage.getItem("login");
    if (to.name === "Login" || loginToken) {
        next()
    } else {
        next('/')
    }


})
export default router